package com.fisherman.test;

import com.fisherman.bean.Boss;
import com.fisherman.bean.Car;
import com.fisherman.bean.Color;
import com.fisherman.config.MainConfigOfLifeCycle;
import com.fisherman.config.MainConfigOfProfile;
import com.fisherman.config.MainConifgOfAutowired;
import com.fisherman.dao.BookDao;
import com.fisherman.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @User: fisherman
 * @Date: 2020/5/20 11:20
 */
public class IOCTest_Autowired {

    //1.使用命令行动态参数：虚拟机参数位置加载-Dspring.profiles.active=test;
    //2.代码的方式激活某种环境

    @Test
    public void test(){
        //1.创建ioc容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConifgOfAutowired.class);

        //2.设置需要激活的环境
        applicationContext.getEnvironment().setActiveProfiles("test","dev");
        //3.注册主配置类
        applicationContext.register(MainConfigOfProfile.class);
        //4.启动刷新容器
        applicationContext.refresh();

        BookService bookService = applicationContext.getBean(BookService.class);
        System.out.println(bookService);

        Boss boss = applicationContext.getBean(Boss.class);
        System.out.println(boss);
        Car car = applicationContext.getBean(Car.class);
        System.out.println(car);

        Color color = applicationContext.getBean(Color.class);
        System.out.println(color);

//        BookDao bookDao = applicationContext.getBean(BookDao.class);
  //      System.out.println(bookDao);

        //关闭容器
        applicationContext.close();
    }
}
